import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/views/layout'
import Login from '@/views/login'

Vue.use(Router)

const constantRoutes = [
    {
        path: '/login',
        component: Login,
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                component: () => import('@/views/dashboard'),
                name: 'Dashboard',
                meta: { title: '首页', icon: 'dashboard' }
            }
        ]
    },
    {
        path: '/user',
        component: Layout,
        redirect: '/user/list',
        meta: { title: '用户管理', icon: 'user' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/user/list'),
                name: 'UserList',
                meta: { title: '用户列表', icon: 'list' }
            },
            {
                path: 'add',
                component: () => import('@/views/user/edit'),
                name: 'UserAdd',
                meta: { title: '添加用户', icon: 'add' },
                hidden: true
            },
            {
                path: 'edit/:id',
                component: () => import('@/views/user/edit'),
                name: 'UserEdit',
                meta: { title: '编辑用户', icon: 'edit' },
                hidden: true
            }
        ]
    },
    {
        path: '/student',
        component: Layout,
        redirect: '/student/list',
        meta: { title: '学生管理', icon: 'peoples' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/student/list'),
                name: 'StudentList',
                meta: { title: '学生列表', icon: 'list' }
            },
            {
                path: 'add',
                component: () => import('@/views/student/edit'),
                name: 'StudentAdd',
                meta: { title: '添加学生', icon: 'add' },
                hidden: true
            },
            {
                path: 'edit/:id',
                component: () => import('@/views/student/edit'),
                name: 'StudentEdit',
                meta: { title: '编辑学生', icon: 'edit' },
                hidden: true
            }
        ]
    },
    {
        path: '/teacher',
        component: Layout,
        redirect: '/teacher/list',
        meta: { title: '教师管理', icon: 'education' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/teacher/list'),
                name: 'TeacherList',
                meta: { title: '教师列表', icon: 'list' }
            },
            {
                path: 'add',
                component: () => import('@/views/teacher/edit'),
                name: 'TeacherAdd',
                meta: { title: '添加教师', icon: 'add' },
                hidden: true
            },
            {
                path: 'edit/:id',
                component: () => import('@/views/teacher/edit'),
                name: 'TeacherEdit',
                meta: { title: '编辑教师', icon: 'edit' },
                hidden: true
            }
        ]
    },
    {
        path: '*',
        redirect: '/404',
        hidden: true
    }
]

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: constantRoutes
})

router.beforeEach((to, from, next) => {
    const token = store.getters.token
    if (to.path === '/login') {
        if (token) {
            next({ path: '/' })
        } else {
            next()
        }
    } else {
        if (!token) {
            next(`/login?redirect=${to.path}`)
        } else {
            next()
        }
    }
})

export default router